package isssr.bean.control;

import isssr.bean.entity.Role;
import isssr.bean.exception.AdminManagementException;
import isssr.bean.exception.NoEntityFoundException;
import isssr.bean.exception.NoMetaEntityIDException;
import isssr.bean.exception.StorageConflictException;
import isssr.bean.exception.StorageFailureException;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

import javax.persistence.EntityNotFoundException;

import org.hibernate.exception.ConstraintViolationException;


public class BusinessRoleManagement implements Serializable {
	

	private AdminManagementException getVerifyException() {
		
		String message = "Insert all the required information: Title, Leve, Owner Permission";
		
		AdminManagementException am = new AdminManagementException(message);
		return am;
	}

	private AdminManagementException getRoleException(Exception ex)
	{	
		String message = "";
		
		if(ex instanceof EntityNotFoundException)
		{
			message = "No matching Role found.";
		}
		else if(ex instanceof NoMetaEntityIDException)
		{
			message = "Role has not been correctly identified.";
		}
		else if(ex instanceof StorageConflictException)
		{
			message = "Duplicate Role: Choose another title.";
		}
		else if(ex instanceof StorageFailureException)
		{
			message = "A Storage error ha  occurred.";
		}
		else
		{
			message = "An unknown error has occurred.";
		}
		
		AdminManagementException am = new AdminManagementException(message);
		am.initCause(ex);
		return am;
	}

	protected boolean verifyRoleFields(Role role)
	{
		boolean fieldsVerified = true;
		
		if(!verifyRoleTitle(role))
			fieldsVerified = false; 
		
		if(role.getLevel() == null)
			fieldsVerified = false; 
		if(role.getGoalOwnerPermission() == null)
			fieldsVerified = false; 
			
		return fieldsVerified;
	}

	protected boolean verifyRoleTitle(Role role) {
		// TODO Auto-generated method stub
		return role.getTitle() == null;
	}
	
	public BusinessRoleManagement() {}
	
	public void insert(Role role) throws AdminManagementException
	{
		if(! verifyRoleFields(role))
			throw getVerifyException();
		
		try
		{
			role.save();
		}
		catch(ConstraintViolationException ex)
		{
			throw getRoleException(ex);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw getRoleException(ex);
		}
		
		return;
	}
	
	public Role read(Role role) throws AdminManagementException
	{
		try
		{
			role.load();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw getRoleException(ex);
		}
		
		return role;
		
	}

	public void delete(String roleTitle) throws AdminManagementException
	{
		Role role = new Role();
		role.setTitle(roleTitle);
		
		try
		{
			role.unsave();
		}
		catch(Exception ex)
		{
			throw getRoleException(ex);
		}
		
		return ;
	}

	public void update(Role role) throws AdminManagementException
	{
		try
		{
			role.update();
		}
		catch(Exception ex)
		{
			throw getRoleException(ex);
		}
		
		return ;
	}

	public ArrayList<Role> roleList(int pageNumber, int pageSize) throws AdminManagementException
	{
		Iterator<Role> roles = null;
		
		try
		{
			roles = (Iterator<Role>)(new Role()).loadSiblings(pageNumber, pageSize);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw getRoleException(ex);
		}
		
		ArrayList<Role> roleList = new ArrayList<Role>();
		
		while(roles.hasNext())
		{
			roleList.add(roles.next());
		}
		return roleList;
	}
}
